package com.frcdi.qms.web.controller.system;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import com.frcdi.qms.common.annotation.Log;
import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.enums.BusinessType;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.system.domain.SysLicense;
import com.frcdi.qms.system.service.ISysLicenseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 授权信息Controller
 *
 * @author semarl
 * @date 2022-03-30
 */
@Api(tags = "授权信息")
@RestController
@RequestMapping("/license")
public class SysLicenseController extends BaseController
{
    @Autowired
    private ISysLicenseService sysLicenseService;

    private byte[] aesKey = "hubeishenmaruanjiankaifa".getBytes(CharsetUtil.CHARSET_UTF_8);

    /**
     * 查询授权信息列表
     */
    @ApiOperation("查询授权信息列表")
    @PreAuthorize("@ss.hasPermi('system:license:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLicense sysLicense)
    {
        startPage();
        List<SysLicense> list = sysLicenseService.selectSysLicenseList(sysLicense);
        return getDataTable(list);
    }

    /**
     * 导出授权信息列表
     */
    @ApiOperation("导出授权信息列表")
    @PreAuthorize("@ss.hasPermi('system:license:export')")
    @Log(title = "授权信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLicense sysLicense) throws Exception
    {
        List<SysLicense> list = sysLicenseService.selectSysLicenseList(sysLicense);
        ExcelUtil<SysLicense> util = new ExcelUtil<SysLicense>(SysLicense.class);
        util.exportExcel(response, list, "授权信息数据");
    }

    /**
     * 获取授权信息详细信息
     */
    @ApiOperation("获取授权信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:license:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysLicenseService.selectSysLicenseById(id));
    }

    /**
     * 新增授权信息
     */
    @ApiOperation("新增授权信息")
    @PreAuthorize("@ss.hasPermi('system:license:add')")
    @Log(title = "授权信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysLicense sysLicense)
    {
        sysLicense.setCreateBy(getUsername());
        return sysLicenseService.insertSysLicense(sysLicense);
    }

    /**
     * 修改授权信息
     */
    @ApiOperation("修改授权信息")
    @PreAuthorize("@ss.hasPermi('system:license:edit')")
    @Log(title = "授权信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysLicense sysLicense)
    {
        return toAjax(sysLicenseService.updateSysLicense(sysLicense));
    }

    /**
     * 删除授权信息
     */
    @ApiOperation("删除授权信息")
    @PreAuthorize("@ss.hasPermi('system:license:remove')")
    @Log(title = "授权信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysLicenseService.deleteSysLicenseByIds(ids));
    }


    @GetMapping(value = "/verifyLicense")
    public AjaxResult verifyLicense()
    {
        return sysLicenseService.verifyLicense();
    }


    @PostMapping("/submitLicense")
    public AjaxResult submitLicense(@RequestBody SysLicense sysLicense)
    {
        return sysLicenseService.submitLicense(sysLicense);
    }


    @GetMapping("/getDeviceInfo")
    public AjaxResult getDeviceInfo()
    {
        String mac = NetUtil.getLocalMacAddress();
        String deviceId = SecureUtil.aes(aesKey).encryptHex(mac);
        return AjaxResult.success(deviceId);
    }

    @GetMapping("/getDeviceInfoForAdd")
    public AjaxResult getDeviceInfoForAdd()
    {
        String mac = NetUtil.getLocalMacAddress();
        String deviceId = SecureUtil.aes(aesKey).encryptHex(mac);
        return AjaxResult.success(deviceId);
    }
}
